<template>
  <div class="login-container">
    <div class="login_box">
      <div data-v-6b0fa6ec="" class="avatar_box">
        <img data-v-6b0fa6ec="" src="../assets/logo.png" alt="" />
      </div>
      <el-form
        :rules="rules"
        :model="ruleForm"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <el-form-item class="btn_right">
          <el-button type="primary" @click="submitForm('ruleForm')" size="small"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')" size="small" type="info"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login_api } from "@/api";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      //获取from表单 然后对表单进行校验

      this.$refs[formName].validate(async valid => {
        // console.log(valid, 5555555);
        //valid 为true 表示校验成功 可以发送请求
        if (valid) {
          /*      // alert("太棒了！，登录成功啦！！");
          // console.log(this.ruleForm);//获用户输入的信息
          this.$axios.post("/Login", this.ruleForm).then(res => {
            console.log(res, 22222222222);
            if (res.data.meta.status == 200) {
              this.$message.success("真聪明！，登录成功啦！！");
              this.$router.push("/Home");
            } else {
              // this.$message.error(res.data.meta.msg);
              this.$message.error("你个笨蛋！登录失败啦!!");
            }
          }); */
          const res = await login_api(this.ruleForm);
          if (res.data.meta.status == 200) {
            this.$message.success("真聪明！，登录成功啦！！");

            // console.log(res, 111111);
            //登录成功本地存储token
            window.sessionStorage.setItem("token", res.data.data.token);
            this.$router.push("/Home");
          } else {
            // this.$message.error(res.data.meta.msg);
            this.$message.error("你个笨蛋！登录失败啦!!");
          }
        } else {
          console.log("你个笨蛋！登录失败啦!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style scoped lang="less">
.login-container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
  .avatar_box {
    width: 130px;
    height: 130px;
    background-color: rgba(238, 238, 238);
    border: 10px solid #fff;
    box-shadow: 0 0 10px #dddddd;
    position: absolute;
    z-index: 1;
    border-radius: 50%;
    overflow: hidden;
    left: 50%;
    top: -80px;
    transform: translate(-50%, 0);
    img {
      width: 100%;
      height: 100%;
    }
  }
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .el-form {
      //   padding: 80px 20px;
      margin-top: 90px;
      padding: 0 20px;
    }
    .btn_right {
      text-align: right;
    }
  }
}
</style>
